package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.TjZtController;
import com.antoco.operation.bigscreen.model.dto.TjZtDTO;
import com.antoco.operation.bigscreen.model.entity.TjZt;
import com.antoco.operation.bigscreen.mapper.TjZtMapper;
import com.antoco.operation.bigscreen.service.autogeneration.intf.TjZtService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/tj-zt")
@RestController
public class TjZtControllerImpl implements TjZtController {
    private final TjZtService tjZtService;
    private final TjZtMapper tjZtMapper;

    public TjZtControllerImpl(TjZtService tjZtService, TjZtMapper tjZtMapper) {
        this.tjZtService = tjZtService;
        this.tjZtMapper = tjZtMapper;
    }

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public TjZtDTO save(@RequestBody TjZtDTO tjZtDTO) {
        TjZt tjZt = tjZtMapper.asEntity(tjZtDTO);
        return tjZtMapper.asDTO(tjZtService.save(tjZt));
    }

    @Override
    @GetMapping("/{id}")
    public TjZtDTO findById(@PathVariable("id") String id) {
        TjZt tjZt = tjZtService.findById(id).orElse(null);
        return tjZtMapper.asDTO(tjZt);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        tjZtService.deleteById(id);
    }

    @Override
    @GetMapping
    public List<TjZtDTO> list() {
        return tjZtMapper.asDTOList(tjZtService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<TjZtDTO> pageQuery(Pageable pageable) {
        Page<TjZt> tjZtPage = tjZtService.findAll(pageable);
        List<TjZtDTO> dtoList = tjZtPage
                .stream()
                .map(tjZtMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, tjZtPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public TjZtDTO update(@RequestBody TjZtDTO tjZtDTO, @PathVariable("id") String id) {
        TjZt tjZt = tjZtMapper.asEntity(tjZtDTO);
        return tjZtMapper.asDTO(tjZtService.update(tjZt, id));
    }
}